package com.sctvcloud.renhe.ui.utils;

import android.text.TextUtils;

import com.sctvcloud.renhe.beans.LiveAdvanceInfo;

import org.w3c.dom.Text;

/**
 * Created by yuxipan on 2017-09-11.
 */

public class LiveConfigParseUtils {

    public static String getHostAddress(String address) {

//        String test = "gocoder://configure/?publishHost=192.168.32.201&publishPort=1935&publishApplication=livecszzzb&publishStream=livecszzzbMjAxNy0wOS0xMSAwMjoxMTo0OA&publishUseTCP=YES&publishUser=sctv";
        if (TextUtils.isEmpty(address)) {
            return "";
        }
        String[] strTemp = address.split("\\u003F");
        if (strTemp.length > 1) {
            String[] paramStr = strTemp[1].split("&");
            for (int i = 0; i < paramStr.length; i++) {
                if (paramStr[i].contains("publishHost")) {
                    String[] str = paramStr[i].split("=");
                    if (str.length > 1) {
                        return str[1];
                    }
                }
            }
        }

        return "";
    }

    public static LiveAdvanceInfo parseAdvanceInfoAddress(String address) {
        if (TextUtils.isEmpty(address)) {
            return null;
        }
        LiveAdvanceInfo info = new LiveAdvanceInfo();
        String[] strTemp = address.split("\\u003F");
        if (strTemp.length > 1) {
            String[] paramStr = strTemp[1].split("&");
            for (int i = 0; i < paramStr.length; i++) {
                String[] str = paramStr[i].split("=");
                if (paramStr[i].contains("publishHost")) {
                    if (str.length > 1) {
                        info.setHost(str[1]);
                    }
                } else if (paramStr[i].contains("publishPort")) {
                    if (str.length > 1) {
                        int port = Integer.parseInt(str[1]);
                        info.setPort(port);
                    }
                } else if (paramStr[i].contains("publishApplication")) {
                    if (str.length > 1) {
                        info.setAppName(str[1]);
                    }
                } else if (paramStr[i].contains("publishStream")) {
                    if (str.length > 1) {
                        info.setStreamName(str[1]);
                    }
                } else if (paramStr[i].contains("publishUseTCP")) {

                } else if (paramStr[i].contains("publishUser")) {
                    if (str.length > 1) {
                        info.setUserInfo(str[1]);
                    }
                }
            }
        }

        return info;
    }


}
